from api_py.test.api_request import *
from api_py.test.settings import base_url, base_headers
from api_py.utils.MD5 import MD5Encode

# 接口1
def api_01_after_each(request, api_type, response):
    pass


def api_02_after_each(request, api_type, response):
    pass


def api_04_after_each(request, api_type, response):
    pass

def api_05_after_each(request, api_type, response):
    json_obj = json.loads(response.body)
    if ('code' not in json_obj) or (json_obj["code"] != 0):
        return

def api_06_after_each(request, api_type, response):
    pass

# 测试用例：插入督导，如果插入成功，则删除该督导
def api_08_after_each(request, api_type, response):
    json_obj = json.loads(response.body)
    if ('code' not in json_obj) or (json_obj["code"] != 0):
        return

    username = request.body["username"]
    request_restore = Request(base_url + "/user/ms/supervisor/{username}", deepcopy(base_headers), {}, {})
    request_restore.headers["Cookie"] = deepcopy(request.headers["Cookie"])
    request_restore.url = request_restore.url.format(username=username)  # 设置url参数
    print(request_restore.url)
    sendDeleteRequest(request_restore.url, request_restore.headers, request_restore.params, request_restore.body)

# 测试用例：插入咨询师，如果插入成功，则删除该咨询师
def api_09_after_each(request, api_type, response):
    json_obj = json.loads(response.body)
    if ('code' not in json_obj) or (json_obj["code"] != 0):
        return

    username = request.body["username"]
    request_restore = Request(base_url + "/user/ms/counselor/{username}", deepcopy(base_headers), {}, {})
    request_restore.headers["Cookie"] = deepcopy(request.headers["Cookie"])
    request_restore.url = request_restore.url.format(username=username)  # 设置url参数
    sendDeleteRequest(request_restore.url, request_restore.headers, request_restore.params, request_restore.body)


def api_10_1_after_each(request, api_type, response):
    pass


def api_10_2_after_each(request, api_type, response):
    pass

# 测试用例：修改督导，如果修改成功，则复原初始数据
def api_11_after_each(request, api_type, response):
    json_obj = json.loads(response.body)
    if ('code' not in json_obj) or (json_obj["code"] != 0):
        return

    role = request.body["role"]
    username = request.body['username']
    url = deepcopy(base_url)
    url += "/user/ms/supervisor/{username}" if role == "ROLE_SUPERVISOR" else "/user/ms/counselor/{username}"

    request_restore = Request(base_url + "/user/ms/supervisor/{username}", deepcopy(base_headers), None, {})
    request_restore.headers["Cookie"] = deepcopy(request.headers["Cookie"])
    request.url = request.url.format(username=username)  # 设置url参数
    sendPostRequest(request_restore.url, request_restore.headers, None, request_restore.body)


# 测试用例：修改咨询师，如果修改成功，则复原初始数据
def api_12_after_each(request, api_type, response):
    json_obj = json.loads(response.body)
    if ('code' not in json_obj) or (json_obj["code"] != 0):
        return

    body = {
        "username": "updateCounselor",
        "password": "updateCounselor",
        "name": "updateCounselor",
        "phoneNumber": "13900000000",
        "idNumber": "350781196403074025",
        "department": "华东师范大学",
        "title": "无",
        "age": "58",
        "gender": "女",
        "email": "a@qq.com",
        "supervisors" : [
            "ff80818184524b500184b3feafaa0240",  # 绑定一个督导
        ]
    }
    body['password'] = MD5Encode(body['password'])
    request_restore = Request(base_url + "/user/ms/counselor", deepcopy(base_headers), None, body)
    request_restore.headers["Cookie"] = deepcopy(request.headers["Cookie"])
    sendPostRequest(request_restore.url, request_restore.headers, None, request_restore.body)


# 测试用例：删除督导，如果删除成功，则插回
def api_13_after_each(request, api_type, response):
    json_obj = json.loads(response.body)
    if ('code' not in json_obj) or (json_obj["code"] != 0):
        return

    arr = request.url.split("/")
    body = {
        "username": arr[len(arr) - 1],
        "password": "supervisor",
        "name": "supervisor",
        "phoneNumber": "15900000000",
        "idNumber": "350781196403078704",
        "department": "华东师范大学",
        "title": "无",
        "age": "58",
        "gender": "女",
        "qualification": "三级",
        "qualificationCode": "22",
        "email": "a@qq.com",
    }
    body['password'] = MD5Encode(body['password'])
    request_restore  = Request(base_url + "/user/ms/supervisor", deepcopy(base_headers), None, body)
    request_restore.headers["Cookie"] = deepcopy(request.headers["Cookie"])
    sendPutRequest(request_restore.url, request_restore.headers, None, request_restore.body)

# 测试用例：删除咨询师，如果删除成功，则插回
def api_14_after_each(request, api_type, response):
    json_obj = json.loads(response.body)
    if ('code' not in json_obj) or (json_obj["code"] != 0):
        return

    arr = request.url.split("/")
    body = {
        "username": arr[len(arr) - 1],
        "password": "counselor",
        "name": "counselor",
        "age": 58,
        "gender": "女",
        "phoneNumber": "15900000000",
        "idNumber": "350781196403078704",
        "department": "华东师范大学",
        "title": "无",
        "supervisors": [
            "ff80818184524b500184b3feafaa0240",  # 绑定一个督导
        ]
    }

    body['password'] = MD5Encode(body['password'])
    request_restore = Request(base_url + "/user/ms/counselor", deepcopy(base_headers), {}, body)
    request_restore.headers["Cookie"] = deepcopy(request.headers["Cookie"])
    sendPutRequest(request_restore.url, request_restore.headers, None, request_restore.body)


def api_15_after_each(request, api_type, response):
    pass

def api_16_after_each(request, api_type, response):
    pass

# 用例：修改密码，如果修改成功，则要改回原来的密码
def api_17_after_each(request, api_type, response):
    json_obj = json.loads(response.body)
    if ('code' not in json_obj) or (json_obj["code"] != 0):
        return

    body = {
        "oldPassword": request.body["password"],
        "password": request.body["oldPassword"],
    }

    request_restore = Request(base_url + "/user/password", deepcopy(base_headers), {}, body)
    request_restore.headers["Cookie"] = deepcopy(request.headers["Cookie"])
    sendPostRequest(request_restore.url, request_restore.headers, None, request_restore.body)

# 测试用例：⼩程序上更新⽤户信息，如果修改成功，需要复原
def api_18_after_each(request, api_type, response):
    json_obj = json.loads(response.body)
    if ('code' not in json_obj) or (json_obj["code"] != 0):
        return

    body = {
        "name": "姓名",
        "phoneNumber": "15900000000",
        "emergencyContact": "紧急联系人",
        "emergencyPhone": "15900000000",
    }

    request_restore = Request(base_url + "/user/update", deepcopy(base_headers), {}, body)
    request_restore.headers["Cookie"] = deepcopy(request.headers["Cookie"])
    sendPostRequest(request_restore.url, request_restore.headers, None, request_restore.body)

def api_20_after_each(request, api_type, response):
    pass


# 测试用例：修改咨询师最大人数，如果修改成功，需要复原
def api_21_after_each(request, api_type, response):
    json_obj = json.loads(response.body)
    if ('code' not in json_obj) or (json_obj["code"] != 0):
        return

    # 该用例成功修改
    request_restore = deepcopy(request)
    request_restore.body["maxConsults"] = 5  # 设置最大咨询人数为5
    send_request_adapter(api_type.value[0]["method"], request_restore)  # 发送复原请求

# 插入排班成功，按照排班id删除该排班
def api_22_after_each(request, api_type, response):
    json_obj = json.loads(response.body)
    if ('code' not in json_obj) or (json_obj["code"] != 0):
        return

    for record in json_obj["result"]:
        request_restore = Request(base_url + "/arrange/{id}", deepcopy(base_headers), {}, {})
        request_restore.headers["Cookie"] = deepcopy(request.headers["Cookie"])
        request_restore.url = request_restore.url.format(id=record["id"]) # 将id写入url
        result =  sendDeleteRequest(request_restore.url, request_restore.headers, None, request_restore.body)

def api_23_after_each(request, api_type, response):
    pass

def api_24_after_each(request, api_type, response):
    pass

def api_25_after_each(request, api_type, response):
    pass
